Systems that employ classes may benefit from having a global state associated with such classes initialized to a known and valid state. To facilitate establishing state, classes may employ initialization logic, which may be implemented in an initialization method. Conventionally, such initialization methods are invoked by a user of the class. But deferring initialization to user invocation of initialization methods can create problems. Thus, execution of initialization methods may be migrated from user control to system and/or programmatic control.
To facilitate such control, classes can define special methods that are to be executed automatically by a runtime during class initialization. However, the automatic execution of such special methods can lead to unexpected problems (e.g., delays, deadlocks). Problems like delays and deadlocks that are caused by automatic execution of initialization code can be exacerbated in multi-threaded environments.
Conventionally, runtime environments may be constrained to attempting to run a class initialization method on the first access to a class. Such conventional implementations may rely on a flag to record and/or monitor whether a class initialization method has been run, and thus, whether the class is initialized. But with complex scenarios (e.g., class dependency loops, race conditions) encountered in complicated thread situations, a flag may not be effective in mitigating problems associated with delays and/or deadlocks. Thus there remains a need for a system and method to mitigate problems associated with automatic execution of class initialization code.